package com.biz.controller.merchant;

import com.biz.model.Hmodel.base.TBaseBrand;
import com.biz.model.common.MessageBox;
import com.biz.service.MService.merchant.MerchantServiceI;
import com.biz.utils.ConfigUtil;
import com.biz.utils.StringUtil;
import com.framework.controller.BaseController;
import com.framework.util.MD5;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

/***
 * 商户平台
 * @author lzq
 */
@Controller
@RequestMapping(value="/merchant")
public class MerchantController extends BaseController{

	@Resource(name = "merchantService")
	private MerchantServiceI merchantService;


	/**
	 * 跳转商户登录
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/toMerchantLogin")
	public ModelAndView toMerchantLogin()throws Exception{
		mv.clear();
		mv.setViewName("merchant/merchantLogin");
		return mv;
	}

	/**
	 * 登录
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/doLogin")
	public void doLogin(HttpServletResponse response, String loginName, String pwd)throws Exception{
		MessageBox box = new MessageBox();
		String loginNameDec = URLDecoder.decode(loginName, "utf-8");
		String pwdMd5 = MD5.md5(pwd);

		box = merchantService.doLoginByPwd(loginNameDec, pwdMd5);
		if(box.isSuccess())
		{
			HttpSession session = this.getRequest().getSession();
			session.setAttribute(ConfigUtil.get("SESSION_MERCHANT_ID"),box.getMsg());
		}

		writeJson(box,response);
	}


	/**
	 * 跳转商户中心
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/toMerchantCenter")
	public ModelAndView toMerchantCenter()throws Exception{
		this.mv.clear();

		try {
			String merchantId = String.valueOf(this.getRequest().getSession().getAttribute(ConfigUtil.get("SESSION_MERCHANT_ID")));
			if(!StringUtil.isNullOrEmpty(merchantId) && !"null".equals(merchantId)) {
				TBaseBrand tBaseBrand = this.merchantService.getTbaseBrandById(merchantId);
				this.mv.addObject("tBaseBrand", tBaseBrand);
				this.mv.setViewName("merchant/merchantCenter");
				return this.mv;
			}

			this.mv.setViewName("merchant/merchantLogin");
		} catch (Exception var3) {
			var3.printStackTrace();
			this.mv.setViewName("merchant/merchantLogin");
		}

		return this.mv;
	}


	/**
	 * 登出
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/toMerchantLayout")
	public ModelAndView toMerchantLayout()throws Exception{
		mv.clear();
		//获取商户id

		HttpSession session = this.getRequest().getSession();
		session.removeAttribute(ConfigUtil.get("SESSION_MERCHANT_ID"));
		mv.setViewName("merchant/merchantLogin");
		return mv;
	}





	/**
	 * 加载核销记录
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/loadRecord")
	public void loadRecord(HttpServletRequest request,HttpServletResponse response)throws Exception{
		String merchantId = String.valueOf(request.getSession()
				.getAttribute(ConfigUtil.get("SESSION_MERCHANT_ID")));
		List<Map<String,Object>> list = merchantService.getRecordByMerchantId(merchantId);

		writeJson(list,response);
	}

	/**
	 * 商户核销
	 */
	@RequestMapping(value="/merchantDoCharge")
	public void merchantDoCharge(HttpServletRequest request,HttpServletResponse response, String code)throws Exception{
		MessageBox box = new MessageBox();
		String merchantId = String.valueOf(request.getSession()
				.getAttribute(ConfigUtil.get("SESSION_MERCHANT_ID")));
		box = merchantService.merchantDoCharge(merchantId, code);

		writeJson(box,response);
	}

}
